package com.jiankangjin.transsystem.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.transsystem.common.Constant;
import com.jiankangjin.transsystem.services.PayOrderServices;
import com.jiankangjin.transsystem.util.StringUtil;
import com.jiankangjin.transsystem.vo.PayOrderVo;

/**  
 * 订单支付 接口服务提供层
 * 访问订单支付 业务逻辑服务层
 */
@Controller
public class PayOrderController {
	private static final Log log = LogFactory.getLog(PayOrderController.class);
	
	@Autowired
	private PayOrderServices payOrderServices;
	
	
	@RequestMapping(value="/pay_order", method=RequestMethod.POST)
	@ResponseBody
	public PayOrderVo payOrder (
			@RequestParam(required = false) String module_id,
			@RequestParam(required = false) String version,
			@RequestParam(required = false) String local_pay_ssn,
			@RequestParam(required = false) String order_id,
			@RequestParam(required = false) String fee,
			@RequestParam(required = false) String currency,
			@RequestParam(required = false) String channel_id,
			@RequestParam(required = false) String buyer_token,
			@RequestParam(required = false) String buyer_voucher,
			@RequestParam(required = false) String channel_type,
			@RequestParam(required = false) String custom_str,
			@RequestParam(required = false) String sign
			) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("module_id=" + module_id);
		sb.append(" version=" + version);
		sb.append(" local_pay_ssn=" + local_pay_ssn);
		sb.append(" order_id=" + order_id);
		sb.append(" fee=" + fee);
		sb.append(" currency=" + currency);
		sb.append(" channel_id=" + channel_id);
		sb.append(" buyer_token=" + buyer_token);
		sb.append(" buyer_voucher=" + buyer_voucher);
		sb.append(" channel_type=" + channel_type);
		sb.append(" custom_str=" + custom_str);
		sb.append(" sign=" + sign);
		if (log.isInfoEnabled()) {
			log.info("订单支付接口：" + sb);
		}
		PayOrderVo payOrderVo = new PayOrderVo();
		if (StringUtil.isEmpty(module_id) || module_id.length()!=6) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("请求模块编号错误");
			return payOrderVo;
		}
		if (StringUtil.isEmpty(local_pay_ssn)) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("本地支付流水号错误");
			return payOrderVo;
		}
		if (StringUtil.isEmpty(order_id)) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("订单编号错误");
			return payOrderVo;
		}
		if (fee == null || !StringUtil.isNumeric(fee) || Integer.parseInt(fee) <= 0) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("支付金额错误");
			return payOrderVo;
		}
		if (StringUtil.isEmpty(buyer_token)) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("支付令牌错误");
			return payOrderVo;
		}
		if (StringUtil.isEmpty(buyer_voucher)) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("支付凭证错误");
			return payOrderVo;
		}
		if (StringUtil.isEmpty(channel_type)) {
			payOrderVo.setResp_status(Constant.RESP_STAT_MSG_0001);
			payOrderVo.setResp_msg("支付渠道类型错误");
			return payOrderVo;
		}
		//验签
		return payOrderServices.payOrder(module_id, version, local_pay_ssn, order_id, fee, currency, channel_id, buyer_token, buyer_voucher, channel_type, custom_str);
	}
}
